import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import { getcookie } from "@/utils/cookie";
import { Message } from "element-ui";
import asyncRouter from "./model/modeladmin/asyncRouter"
import getRole from './model/modeladmin/userrole';

Vue.use(VueRouter);

const allowList = ["/"];

const routes: Array<RouteConfig> = [
  {
    path: "/",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/login/login.vue")
  },
];

const router = new VueRouter({
  routes
});

// 动态添加路由
router.addRoutes(asyncRouter)

// 路由守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || "管理后台";
  if (allowList.includes(to.path)) {
    getRole()
    next();
  } else {
    if (!getcookie()) {
      Message({
        type: "error",
        message: "请先登录"
      });
      next({
        path: "/"
      });
    } else {
      getRole()
      next();
    }
  }
});

export default router;
